package com.xiaotao.service.impl;

import com.xiaotao.entity.Scoring;
import com.xiaotao.mapper.ScoringMapper;
import com.xiaotao.service.ScoringService;
import com.xiaotao.util.ResponseEnum;
import com.xiaotao.util.ResponseUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ScoringServiceImpl implements ScoringService {

    @Resource
    private ScoringMapper scoringMapper;

    @Override
    public ResponseUtil add(Scoring scoring) {
        if (scoring == null || scoring.getId() == null || scoring.getScoring() == null) {
            return ResponseUtil.get(ResponseEnum.FAIL, "参数不完整");
        }
        int rows = scoringMapper.add(scoring);
        return rows > 0 ? ResponseUtil.get(ResponseEnum.OK, "新增成功") :
                ResponseUtil.get(ResponseEnum.FAIL, "新增失败");
    }

    @Override
    public ResponseUtil queryByUserId(Integer userId) {
        if (userId == null || userId <= 0) {
            return ResponseUtil.get(ResponseEnum.FAIL, "用户ID无效");
        }
        Scoring scoring = scoringMapper.queryByUserId(userId);
        return scoring != null ? ResponseUtil.get(ResponseEnum.OK, scoring) :
                ResponseUtil.get(ResponseEnum.FAIL, "无评分记录");
    }

    @Override
    public ResponseUtil update(Scoring scoring) {
        if (scoring == null || scoring.getId() == null) {
            return ResponseUtil.get(ResponseEnum.FAIL, "用户ID不能为空");
        }
        int rows = scoringMapper.update(scoring);
        return rows > 0 ? ResponseUtil.get(ResponseEnum.OK, "更新成功") :
                ResponseUtil.get(ResponseEnum.FAIL, "更新失败");
    }
}